package com.demo.common.redis;

import cn.hutool.core.lang.Assert;
import cn.hutool.json.JSONUtil;
import com.alibaba.fastjson.JSONObject;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.common.collect.Lists;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.tuple.Pair;
import org.springframework.util.StringUtils;

import java.util.List;

/**
 * <p>
 * 消息处理工具类
 * </p>
 *
 * @author molong
 * @date 2022/6/22
 */
@Slf4j
public class MsgUtil {

    /**
     * 反序列化
     * @param json 数据
     * @return 反序列化结果
     */
    public static <T> T deserialization(String json, ObjectMapper objectMapper, TypeReference<T> typeReference){
        T t = null;
        if(StringUtils.hasText(json)){
            Assert.notNull(objectMapper, "反序列化器不能为空");
            try {
                t = objectMapper.readValue(json, typeReference);
            } catch (JsonProcessingException e) {
                log.error("反序列化消息失败：", e);
            }
        }
        return t;
    }

    public static void main(String[] args) {
        List<Pair<Integer, String>> urls = Lists.newArrayList();
        urls.add(Pair.of(1, "http://file.daqsoft.com/uploadfile/server/jqgk-zhzsgjjfjmsq/cloud-platform/杭州西湖2.jpg"));
        urls.add(Pair.of(1,"http://file.daqsoft.com/uploadfile/server/jqgk-zhzsgjjfjmsq/cloud-platform/杭州西湖2.jpg"));
        urls.add(Pair.of(4,"http://file.daqsoft.com/uploadfile/server/jqgk-zhzsgjjfjmsq/cloud-platform/杭州西湖2.jpg"));
        urls.add(Pair.of(2,"http://file.daqsoft.com/uploadfile/server/jqgk-zhzsgjjfjmsq/cloud-platform/杭州西湖2.jpg"));
        urls.add(Pair.of(3,"http://file.daqsoft.com/uploadfile/server/jqgk-zhzsgjjfjmsq/cloud-platform/杭州西湖2.jpg"));
        String json = JSONUtil.toJsonStr(urls);
        List<Pair> cl =   JSONObject.parseArray(json, Pair.class);
        System.out.println(cl);
    }
}
